package com.iyao_app.fragment;

import android.content.Intent;
import android.icu.text.Replaceable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iyao_app.Friends.ChatActivity;
import com.iyao_app.R;
import com.iyao_app.im.adapter.AttentionFriendAdapter;
import com.iyao_app.entry.MyInterestBean;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.User;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.info.PersonInfoActivity;
import com.iyao_app.mvp.other.OtherHomeActivity;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by xue on 2017/11/24.
 */

public class FriendFragment extends Fragment implements AttentionFriendAdapter.ONCLICK {

    int page = 1, pageSize = 1000, type = 3;
    @BindView(R.id.iv_abnormal)
    ImageView mIvAbnormal;
    private AttentionFriendAdapter attentionFriendAdapter;
    public List<MyInterestBean> modelList = new ArrayList<>();
    @BindView(R.id.friend_hint)
    TextView mFriendHint;
    @BindView(R.id.friend_no)
    TextView mFriendNo;
    @BindView(R.id.recyclerview_friend)
    RecyclerView mRecyclerviewFriend;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    Unbinder unbinder;
    private String id;
    private String nickname;
    private String favicon;
    private int sex;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friend, null);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRefreshLayout.setEnableRefresh(false);

        if (getArguments() != null) {
            id = getArguments().getString(Contants.OTHER_FIELD);
        }
        if (TextUtils.isEmpty(id)) {
            User user = DbUtil.getUser();
            id = user.id;
            nickname = user.nickname;
            favicon = user.avatarUrl;
            sex = user.sex;
        }
        InterestMy();
    }

    private void InterestMy() {

        NetUtils.getInstance().getFriends(id,nickname,favicon,sex, page, pageSize, type, new NetCallBack() {
            @Override
            public void onSuccess(String message, final int code, String response, final ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        modelList = (List<MyInterestBean>) model.getModelList();
                        if (modelList.size() != Contants.ZERO) {
                            mRecyclerviewFriend.setLayoutManager(new LinearLayoutManager(getActivity()));
                            attentionFriendAdapter = new AttentionFriendAdapter(getActivity(), mRecyclerviewFriend, modelList, FriendFragment.this);
                            attentionFriendAdapter.setOnChatListener(new AttentionFriendAdapter.OnChatListener() {
                                @Override
                                public void chat(int position) {
                                    Intent intent = new Intent(getActivity(), ChatActivity.class);
                                    intent.putExtra("username", modelList.get(position).getFriendId() + "");
                                    intent.putExtra("nickname", modelList.get(position).getNickName() + "");
                                    intent.putExtra("favicon", modelList.get(position).getFavicon() + "");
                                    getActivity().startActivity(intent);
                                }
                            });

                            mRecyclerviewFriend.setAdapter(attentionFriendAdapter);
                        } else {
                            mFriendHint.setVisibility(View.VISIBLE);
                            mFriendNo.setVisibility(View.VISIBLE);
                            mIvAbnormal.setVisibility(View.VISIBLE);
                            mRecyclerviewFriend.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, MyInterestBean.class);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onclick(int position) {
        Intent intent = new Intent(getContext(), OtherHomeActivity.class);
        intent.putExtra(Contants.TYPE, String.valueOf(modelList.get(position).getFriendId()));
        startActivity(intent);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        // TODO Auto-generated method stub
        super.onHiddenChanged(hidden);
        if (!hidden) {
            InterestMy();
        }
    }
}
